/*
 * @Author: 刘润泽
 * @Date: 2022-11-08 18:34:08
 * @LastEditors: 刘润泽
 * @LastEditTime: 2023-02-26 22:36:25
 * @Description: 
 */
package server.business.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.business.pojo.*;
import server.business.service.TransfusionApplicationInformationService;
import server.business.service.BloodInformationService;
import server.business.service.MsgTextService;
import server.business.service.OverallBloodService;
import server.business.service.PatientService;
import server.business.utils.results.lrz.Result;

import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author 刘润泽
 * @date 2022/8/14 12:50
 */

@RestController
@RequestMapping("/transfusionApplicationInformation")
public class TransfusionApplicationInformationController {

    /**
     * 引入输血申请单相关功能
     */
    @Autowired
    private TransfusionApplicationInformationService transfusionApplicationInformationService;

    /**
     * 引入通知信息获取相关功能
     */
    @Autowired
    MsgTextService msgTextService;

    /**
     * 引入病人信息相关功能
     */
    @Autowired
    PatientService patientService;

    /**
     * 引入血液信息表相关功能
     */
    @Autowired
    BloodInformationService bloodInformationService;

    /**
     * 引入整体输血信息表相关功能
     */
    @Autowired
    OverallBloodService overallBloodService;

    /**
     * 返回所有配血信息
     *
     * @return
     */
    @GetMapping("/getAll")
    public Result getAll() {
        List<TransfusionApplicationInformation> TransfusionApplicationInformations = transfusionApplicationInformationService
                .getAll();
        return Result.success(TransfusionApplicationInformations);
    }

    /**
     * 用于分页
     *
     * @param currentPage
     * @param pageSize
     * @param TransfusionApplicationInformation
     * @return
     */
    @GetMapping("/normal/{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize,
            TransfusionApplicationInformation TransfusionApplicationInformation) {
        IPage<TransfusionApplicationInformation> page = transfusionApplicationInformationService.getPage(currentPage,
                pageSize,
                TransfusionApplicationInformation);
        // 如果当前页码值大于了总页码值，就重新执行查询操作，使用最大页码值作为当前页码值
        if (currentPage > page.getPages()) {
            page = transfusionApplicationInformationService.getPage((int) page.getPages(), pageSize,
                    TransfusionApplicationInformation);
        }
        return Result.success(page);
    }

    /**
     * 根据血液编号更改血液分配表状态
     * 
     * @param transfusionApplicationInformation
     * @return
     */
    @RequestMapping(value = "/normal/setStateByBloodNumber", method = RequestMethod.PUT)
    public Result setStateByBloodNumber(
            @RequestBody TransfusionApplicationInformation transfusionApplicationInformation) {
        String bloodNumber = transfusionApplicationInformation.getBloodNumber();
        String state = transfusionApplicationInformation.getState();
        transfusionApplicationInformationService.setStateByBloodNumber(bloodNumber, state);
        Result result = new Result();
        result.setCode("200");
        result.setMsg("血液分配表状态更改成功");
        return result;
    }

    /**
     * 血液发血后添加一条待取血记录并更改自体输血表血液状态信息
     * 
     * @param transfusionApplicationInformation
     * @return
     */
    @RequestMapping(value = "/normal/addRecord", method = RequestMethod.POST)
    public Result addRecord(@RequestBody TransfusionApplicationInformation transfusionApplicationInformation) {
        transfusionApplicationInformationService.addRecord(transfusionApplicationInformation);
        return Result.success("配血记录添加成功");
    }

    /**
     * 检测血液分配表中是否已经有了记录
     * 
     * @param patientIdCard
     * @return
     */
    @GetMapping(value = "/normal/checkRecords")
    public Result checkRecords(String patientIdcard, String transfusionNumber) {
        // 先通过身份证号找到patient_id
        Long patientId = patientService.getIdByIdcard(patientIdcard);
        Result result = new Result();
        if (patientId == null) {
            result.setData(null);
            result.setMsg("未找到病人身份信息");
            result.setCode("100");
            return result;
        }
        // 再通过patient_id查找transfusion表中是否有了正在交叉配血的记录
        List<TransfusionApplicationInformation> list = transfusionApplicationInformationService
                .checkRecords(patientId.toString(), transfusionNumber);

        if (list.size() == 0) {
            result.setData(null);
            result.setCode("200");
            return result;
        }
        Integer flag = 0;
        for (TransfusionApplicationInformation transfusionApplicationInformation : list) {
            System.out.println(transfusionApplicationInformation.getState());
            if (transfusionApplicationInformation.getState() == null) {
                flag = 1;
                break;
            }
            if (transfusionApplicationInformation.getState().equals("8")) { // 8代表已发血
                flag = 1;
                break;
            }
        }
        System.out.println(flag);
        // 代表已经有记录了
        if (flag == 1) {
            result.setData(null);
            result.setMsg("已经分配了");
            result.setCode("100");
        } else {
            result.setCode("200");
        }
        return result;
    }

    /**
     * 批量删除
     * 
     * @param ids
     * @return
     */
    @RequestMapping("/normal/batchDelete")
    @ResponseBody
    public Result batchDelete(@RequestBody ArrayList<Long> ids) {
        System.out.println(ids);
        boolean success = transfusionApplicationInformationService.removeByIds(ids);
        Result result = new Result();
        if (success) {
            result.setCode("200");
            result.setMsg("删除成功");
        } else {
            result.setCode("500");
            result.setMsg("删除失败");
        }
        return result;
    }
}
